package com.derbysoft.lowcostadapter.dswitch2.service.impl;

import com.derbysoft.lowcostadapter.dswitch2.service.MonitorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;

public class MonitorServiceImpl implements MonitorService {
    private static Logger logger = LoggerFactory.getLogger(MonitorServiceImpl.class);

    private static final int sizeOfMemoryTest = 4 * 1024 * 1024;

    @Override
    public void doTest() {
        doDatabaseTest();
        doMemoryTest();
        logger.info("All test OK!");
    }

    private void doMemoryTest() {
        logger.info("Begin memory test");
        byte[] bytes = new byte[sizeOfMemoryTest];
        try {
            logger.info(String.format("End memory test %d" + new String(bytes, "UTF-8")));
        } catch (UnsupportedEncodingException e) {
            PrintWriter writer = new PrintWriter(new StringWriter());
            e.printStackTrace(writer);
            logger.error(writer.toString());
        }
    }

    private void doDatabaseTest() {
        logger.info("Begin database test");
        logger.info("End database test");
    }
}
